//
//  BQProductTableViewCell.m
//  BeeQuick
//
//  Created by AaronWei on 2017/7/9.
//  Copyright © 2017年 BecomeRich. All rights reserved.
//

#import "BQProductTableViewCell.h"
//#import "BQClickShopingView.h"
#import "BQProductModel.h"
#import "BQShopModelArrayManager.h"

@interface BQProductTableViewCell ()

//@property (weak, nonatomic) BQClickShopingView *clickShopingView;

@property (weak, nonatomic) IBOutlet UIImageView *imgImageView;

@property (weak, nonatomic) IBOutlet UIImageView *isXfImageView;

@property (weak, nonatomic) IBOutlet UILabel *nameLabel;

@property (weak, nonatomic) IBOutlet UIImageView *isPmImageView;

@property (weak, nonatomic) IBOutlet UILabel *specificsLabel;

@property (weak, nonatomic) IBOutlet UILabel *priceLabel;

@property (weak, nonatomic) IBOutlet UILabel *market_priceLabel;

@property (weak, nonatomic) IBOutlet UIButton *increaseButton;

@property (weak, nonatomic) IBOutlet UIButton *reduceButton;

@property (weak, nonatomic) IBOutlet UILabel *countLabel;

@end

@implementation BQProductTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        
        [self addObserver:self forKeyPath:@"model.count" options:NSKeyValueObservingOptionNew context:nil];
        
    }
    
    return self;
    
}

- (void)awakeFromNib {
    [super awakeFromNib];
    
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    
//    BQClickShopingView *shopingView = [BQClickShopingView clickShopingView];
//    
//    _clickShopingView = shopingView;
//    
//    [self.contentView addSubview:shopingView];
//    
//    [shopingView mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.trailing.equalTo(self).offset(-8);
//        make.bottom.equalTo(self.imgImageView.mas_bottom).offset(-8);
//        make.width.mas_equalTo(80);
//        make.height.mas_equalTo(27);
//    }];
//    
//    [shopingView addTarget:self action:@selector(didClickShopingView:) forControlEvents:UIControlEventValueChanged];
    
    [self addObserver:self forKeyPath:@"model.count" options:NSKeyValueObservingOptionNew context:nil];
    
}

- (void)setModel:(BQProductModel *)model {
    
    _model = model;
    
//    _clickShopingView.count = model.count;
    
    _reduceButton.hidden = !model.count;
    
    _countLabel.hidden = !model.count;
    
    _countLabel.text = [NSString stringWithFormat:@"%ld", model.count];
    
    [self.imgImageView sd_setImageWithURL:[NSURL URLWithString:model.img]];
    
    self.nameLabel.text = model.name;
    
    self.specificsLabel.text = model.specifics;
    
    self.priceLabel.text = [NSString stringWithFormat:@"¥%.2f", model.price];
    
    NSDictionary *attribtDic = @{NSStrikethroughStyleAttributeName: [NSNumber numberWithInteger:NSUnderlineStyleSingle]};
    
    NSMutableAttributedString *attribtStr = [[NSMutableAttributedString alloc]initWithString:[NSString stringWithFormat:@"¥%.2f", model.market_price] attributes:attribtDic];
    
    self.market_priceLabel.attributedText = attribtStr;
    
    if (model.had_pm) {
        self.isPmImageView.hidden = NO;
    } else {
        self.isPmImageView.hidden = YES;
    }
    
    if (!model.is_xf) {
        self.isXfImageView.hidden = YES;
        self.nameLabel.transform = CGAffineTransformMakeTranslation(-34, 0);
    } else {
        self.isXfImageView.hidden = NO;
        self.nameLabel.transform = CGAffineTransformIdentity;
    }
    
}

- (IBAction)clickButton:(UIButton *)sender {
    
    if (sender == _reduceButton) {
        
//        _clickIncreaseButton = NO;
        
        [[BQShopModelArrayManager manager] shopModelArrayManagerReduceWithModel:self.model];
        
        self.model.count--;
        
//        [self sendActionsForControlEvents:UIControlEventValueChanged];
        
    } else {
        
        if (self.model.count >= self.model.number) {
            
            NSString * str = [self.model.name stringByAppendingString:@"库存不足了,请等待货源充足再购买~"];
            
            [SVProgressHUD showImage:[UIImage imageNamed:@"v2_orderSuccess"] status:str];
                        
            [SVProgressHUD dismissWithDelay:1.0];
            
            return;
        }
        
//        _clickIncreaseButton = YES;
        
        [[BQShopModelArrayManager manager] shopModelArrayManagerAddWithModel:self.model];
        
        self.model.count++;
                
//        [self sendActionsForControlEvents:UIControlEventValueChanged];
        
        CGPoint center = self.imgImageView.center;
        
        CGPoint targetPoint = [self.imgImageView convertPoint:center toView:kWindow];
        
        NSValue *targetValue = [NSValue valueWithCGPoint:targetPoint];
        
        [[NSNotificationCenter defaultCenter] postNotificationName:ToValueNotificationName object:self.model userInfo:@{@"point":targetValue, @"size": [NSValue valueWithCGSize:self.imgImageView.frame.size]}];
        
    }
    
    [[BQShopModelArrayManager manager] tabBarShowCount];
    
//    _countLabel.text = [NSString stringWithFormat:@"%ld", self.model.count];
    
}


//- (void)didClickShopingView:(BQClickShopingView *)clickShopingView {
//    
//    NSLog(@"...");
//    
//    _model.count = clickShopingView.count;
//    
//    if (clickShopingView.isClickIncreaseButton) {
//        
//        CAAnimationGroup *animationGroup = [CAAnimationGroup animation];
//        
//        CABasicAnimation *basicAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
//        
//        basicAnimation.toValue = @(2 * M_PI);
//        
//    }
//    
//}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context {
    
    if ([keyPath isEqualToString:@"model.count"]) {
        
        _reduceButton.hidden = !self.model.count;
        
        _countLabel.hidden = !self.model.count;
        
        self.countLabel.text = [NSString stringWithFormat:@"%zd", self.model.count];
        
    }
    
}

-(void)dealloc {
    
    NSLog(@"%s", __func__);
    
    [self removeObserver:self forKeyPath:@"model.count"];
    
}

@end
